// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
// 创建云数据库对象前，需要先init初始化
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  //   event,
  //   openid: wxContext.OPENID,
  //   appid: wxContext.APPID,
  //   unionid: wxContext.UNIONID,


  // 获取日期
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1;
  var day = myDate.getDate();
  var hours = myDate.getHours() + 8
  var minutes = myDate.getMinutes()
  if (month < 10) { month = "0" + month }
  if (day < 10) { day = "0" + day; }
  if (hours < 10) { hours = "0" + hours; }
  if (minutes < 10) { minutes = "0" + minutes; }

  var datetimeStr = year + "-" + month + "-" + day + " " + hours + ":" + minutes
  var time = hours + ":" + minutes
  // var datetime = Date.parse(datetimeStr)
  let backlog = await db.collection('backlog').where({
    datetime: datetimeStr,
    is_remind: true,
    is_reminded: false
  })
    .get().then(res => {
      res.data.forEach(function (item) {
        cloud.callFunction({	//调用发送模板消息的云函数，对应上边的那个发送函数
          name: "subRemindMessage",
          data: {
            openid: item.openid,
            taskName: item.content,
            remindTime: item.datetime,
          }
        }).then(res => {
          console.log("发送成功", res);
          db.collection('backlog').doc(item._id).update({
            data: {
              is_reminded: true
            },
            success: function (res) {
              console.log("更新成功", res);
              return res
            },
            fail: function (res) {
              // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
              console.log("更新失败", res);
              return res
            }
          })
        }).catch(res => {
          console.log("发送失败", res);
          return res
        })
      })
      return res
    })

  let schedule = await db.collection('schedule').where({
    datetime: time,
    is_remind: true,
  })
    .get().then(res => {
      res.data.forEach(function (item) {
        cloud.callFunction({	//调用发送模板消息的云函数，对应上边的那个发送函数
          name: "subRemindMessage",
          data: {
            openid: item.openid,
            taskName: item.content,
            remindTime: item.datetime,
          }
        }).then(res => {
          console.log("发送成功", res);
          // db.collection('schedule').doc(item._id).update({
          //   data: {
          //     is_reminded: true
          //   },
          //   success: function (res) {
          //     console.log("更新成功", res);
          //     return res
          //   },
          //   fail: function (res) {
          //     // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
          //     console.log("更新失败", res);
          //     return res
          //   }
          // })
        }).catch(res => {
          console.log("发送失败", res);
          return res
        })
      })
      return res
    })

  return {
    backlog: backlog,
    schedule: schedule,
    datetimeStr: datetimeStr,
    time: time
  }
  
  
}